package com.njcb.game.service;

import com.njcb.ams.util.AmsUtils;
import com.njcb.game.pojo.RegisterInput;
import com.njcb.game.repository.dao.GameRegisterDAO;
import com.njcb.game.repository.entity.GameRegister;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * @author LOONG
 */
@Service
public class RegisterService {
    @Autowired
    private GameRegisterDAO gameRegisterDAO;

    public String register(RegisterInput input){
        GameRegister gameRegister = gameRegisterDAO.selectByNo(input.getRunnersNo());
        String token = UUID.randomUUID().toString();
        if(null == gameRegister){
            gameRegister = new GameRegister();
            gameRegister.setToken(token);
        }
        if(AmsUtils.isNull(gameRegister.getToken())){
            gameRegister.setToken(token);
        }
        gameRegister.setRunnersNo(input.getRunnersNo());
        gameRegister.setRunnersName(input.getRunnersName());
        gameRegister.setServerAddr(input.getServerAddr());
        gameRegisterDAO.saveOrUpdateByPrimaryKey(gameRegister);
        return gameRegister.getToken();
    }
}
